﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GS.Cricket.Service.Impl.NHibernate;
using NHibernate;
using NHibernate.Criterion;

namespace GS.Cricket.Service.Impl
{
    public class MatchPlayerService : IMatchPlayerService
    {

        #region IMatchPlayerService Members

        public int Add(Entities.MatchPlayer matchPlayer)
        {
            return (int)matchPlayer.Add<Entities.MatchPlayer>();
        }

        public void Update(Entities.MatchPlayer matchPlayer)
        {
            matchPlayer.Update<Entities.MatchPlayer>();
        }

        public void Delete(int matchPlayerId)
        {
            var existing = (Entities.MatchPlayer)matchPlayerId.GetById<Entities.MatchPlayer>();
            if (existing != null)
            {
                existing.Delete<Entities.MatchPlayer>();
            }            
        }

        public Entities.MatchPlayer GetById(int matchPlayerId)
        {
            return (Entities.MatchPlayer)matchPlayerId.GetById<Entities.MatchPlayer>();
        }

        public IEnumerable<Entities.MatchPlayer> GetByPlayer(int playerId)
        {
            using (ISession session = NHibernateHelper<Entities.MatchPlayer>.OpenSession())
            {
                var result = session
                    .CreateCriteria(typeof(Entities.MatchPlayer))
                    .Add(Restrictions.Eq("PlayerId", playerId))
                    .List<Entities.MatchPlayer>();
                return result;
            }
        }

        public IEnumerable<Entities.MatchPlayer> GetByMatch(int matchId)
        {
            using (ISession session = NHibernateHelper<Entities.MatchPlayer>.OpenSession())
            {
                var result = session
                    .CreateCriteria(typeof(Entities.MatchPlayer))
                    .Add(Restrictions.Eq("MatchId", matchId))
                    .List<Entities.MatchPlayer>();
                return result;
            }
        }

        public IEnumerable<Entities.MatchPlayer> GetAllMatchPlayers()
        {
            return this.GetAll<Entities.MatchPlayer>();
        }

        #endregion
    }
}
